package com.hluck.materialstudy.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.bumptech.glide.Glide
import com.hluck.materialstudy.R
import com.hluck.materialstudy.data.models.Fruit
import com.hluck.materialstudy.databinding.FruitItemLayoutBinding
import com.hluck.materialstudy.ui.activity.FruitActivity

class FruitAdapter(val context: Context,val list: ArrayList<Fruit>):Adapter<FruitAdapter.ViewHolder>() {

    inner class ViewHolder(val fruitItemBinding:FruitItemLayoutBinding):RecyclerView.ViewHolder(fruitItemBinding.root){
        val imageView = fruitItemBinding.imgFruitImage
        val textView = fruitItemBinding.tvFruitName

        fun bind(fruit:Fruit){
            fruitItemBinding.fruit = fruit
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<FruitItemLayoutBinding>(LayoutInflater.from(parent.context),R.layout.fruit_item_layout,parent,false)
        val holder = ViewHolder(binding)
        holder.itemView.setOnClickListener {
            FruitActivity.startFruitActivity(context,list[holder.adapterPosition])
        }
        return holder
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = list[position]
        holder.bind(fruit)
//        holder.textView.text = fruit.name
//        Glide.with(context).load(fruit.imageId).into(holder.imageView)
    }


}